////
//
/// \file   ./src/immediate_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/immediate_node.hpp>

#include <string>

#include <boost/scoped_ptr.hpp>

#include <inc/parser/immediate_number_node.hpp>

namespace inc {
namespace parser {

using boost::scoped_ptr;

/// \brief  Parse any immediate value
/// \author Chandler Carruth
/// \date   2007.07.26
///
/// This parses out any immediate value by selecting each different type, and
/// attempting to parse using it.

bool
ImmediateNode::parse( const iterator &first,
                      const iterator &last,
                      iterator &last_parsed )
{
  assert( get_text().empty() && !node
          && "This node was parsed more than once!" );

  //  Attempting to parse a label node.
  boost::scoped_ptr<ParseTreeNode> number( new ImmediateNumberNode );
  if( number->parse( first, last, last_parsed ) )
  {
    set_text( number->get_text() );
    node.swap( number );
    return true;
  }

  //  No valid parse found.
  last_parsed = first;
  return false;
}

/// \brief  Convenience getter for a Label sub-node
/// \author Chandler Carruth
/// \date   2008.05.16

ImmediateNumberNode* ImmediateNode::get_number() const
{
  return dynamic_cast<ImmediateNumberNode*>( node.get() );
}

} //  end parser namespace
} //  end inc namespace
